package com.atguigu.tingshu.album.service.impl;


import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.MqOpsService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class MqOpsServiceImpl implements MqOpsService {

    @Autowired
    private TrackStatMapper trackStatMapper;
    @Autowired
    private AlbumStatMapper albumStatMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int trackAndAlbumStateTypeUpdate(TrackStatMqVo trackStatMqVo) {

        Long trackId = trackStatMqVo.getTrackId();
        Long albumId = trackStatMqVo.getAlbumId();
        Integer count = trackStatMqVo.getCount();
        String statType = trackStatMqVo.getStatType();

        //修改专辑和声音的播放量
        int flag= 0;
        try {
            flag = trackStatMapper.trackAndAlbumStateTypeUpdate(trackId,count,statType);

            if (flag>0){
                log.info("修改专辑和声音的播放量成功");
            }else {
                log.info("修改专辑和声音的播放量失败");
            }
            //修改专辑的播放量
            if (albumId!=null){
                int i= albumStatMapper.updateAlbumPlayCount(albumId,"0401",count);
                if (i>0){
                    log.info("修改专辑的播放量成功");
                }else {
                    log.info("修改专辑的播放量失败");
                }
            }

        } catch (Exception e) {
            throw new GuiguException(201,"业务逻辑操作数据库失败");
        }

        //int k= 1/0;//模拟业务出现异常
        return flag;
    }
}
